package com.cn.goodsadmin.controller;

import com.cn.goodsadmin.po.Share;
import com.cn.goodsadmin.po.Sharedic;
import com.cn.goodsadmin.service.ShareService;
import com.cn.goodsadmin.util.ErrorMsg;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin(origins = "*", maxAge = 3600)
@Controller
public class ShareController {


    @Autowired
    ShareService shareService;

    @LoginRequired
    @RequestMapping(value = "/addShare", method = RequestMethod.POST, produces = "application/json;charset=utf-8",
            consumes = "application/json;charset=utf-8")
    @ResponseBody//表示返回的是个json对象会经过配置文件转换
    public SupplyMap addShare(@RequestBody Share share) {
        SupplyMap supplyMap = new SupplyMap();
        try {
            shareService.saveShare(share);
        } catch (Exception e) {
            supplyMap.setEorrInfo(ErrorMsg.STATUS_DATABASE_ERROR, ErrorMsg.MSG_DATABASE_ERROR);
        }
        return supplyMap;
    }

    @LoginRequired
    @RequestMapping(value = "/updateShare", method = RequestMethod.POST, produces = "application/json;charset=utf-8",
            consumes = "application/json;charset=utf-8")
    @ResponseBody//表示返回的是个json对象会经过配置文件转换
    public SupplyMap updateShare(@RequestBody Share share) {
        SupplyMap supplyMap = new SupplyMap();
        try {
            shareService.updateShare(share);
        } catch (Exception e) {
            supplyMap.setEorrInfo(ErrorMsg.STATUS_DATABASE_ERROR, ErrorMsg.MSG_DATABASE_ERROR);
        }
        return supplyMap;
    }


    @LoginRequired
    @RequestMapping(value = "/deleteShare", method = RequestMethod.POST, produces = "application/json;charset=utf-8",
            consumes = "application/json;charset=utf-8")
    @ResponseBody//表示返回的是个json对象会经过配置文件转换
    public SupplyMap deleteShare(@RequestBody Share share) {
        SupplyMap supplyMap = new SupplyMap();
        try {
            shareService.deleteShare(share);
        } catch (Exception e) {
            supplyMap.setEorrInfo(ErrorMsg.STATUS_DATABASE_ERROR, ErrorMsg.MSG_DATABASE_ERROR);
        }
        return supplyMap;
    }

    @LoginRequired
    @RequestMapping(value = "/getallShare", method = RequestMethod.GET,params = {"appid"})
    @ResponseBody//表示返回的是个json对象会经过配置文件转换
    public SupplyMap getallShare(int appid) {
        SupplyMap supplyMap = new SupplyMap();
        try {
            List<Share> shares = shareService.findAllShare(appid);
            if (shares != null) {
                supplyMap.put("shares", shares);
            }
        } catch (Exception e) {
            supplyMap.setEorrInfo(ErrorMsg.STATUS_DATABASE_ERROR, ErrorMsg.MSG_DATABASE_ERROR);
        }
        return supplyMap;
    }

    @LoginRequired
    @RequestMapping(value = "/getallShareDic", method = RequestMethod.GET)
    @ResponseBody//表示返回的是个json对象会经过配置文件转换
    public SupplyMap getallShareDic() {
        SupplyMap supplyMap = new SupplyMap();
        try {
            List<Sharedic> sharedics = shareService.findAllShareDic();
            if (sharedics != null) {
                supplyMap.put("sharedics", sharedics);
            }
        } catch (Exception e) {
            supplyMap.setEorrInfo(ErrorMsg.STATUS_DATABASE_ERROR, ErrorMsg.MSG_DATABASE_ERROR);
        }
        return supplyMap;
    }
}
